package com.awesome.uidemo.customview.systemview

import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.awesome.common.util.log.ShowLogUtil
import com.awesome.uidemo.R
import com.awesome.uidemo.util.DisplayUtil

/**
 * Author: MrQinshou
 * Email:cqflqinhao@126.com
 * Date: 2021/4/1 17:44
 * Description:TextView with drawable.
 */
class DrawableTextView : ConstraintLayout {
    val ivImg: ImageView
    val tvText: TextView
    val tvUnreadCount: TextView

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView)
        val ivImgLayoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        val tvUnreadCountLayoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        val tvTextLayoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        // 图片
        ivImg = ImageView(context)
        ivImg.id = R.id.iv_img
        ivImgLayoutParams.width = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableWidth, LayoutParams.WRAP_CONTENT)
        ivImgLayoutParams.height = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableHeight, LayoutParams.WRAP_CONTENT)
        ivImgLayoutParams.leftMargin = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableMarginLeft, 0)
        ivImgLayoutParams.topMargin = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableMarginTop, 0)
        ivImgLayoutParams.rightMargin = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableMarginRight, 0)
        ivImgLayoutParams.bottomMargin = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_drawableMarginBottom, 0)
        ivImg.scaleType = ImageView.ScaleType.CENTER_INSIDE
        ivImg.setImageResource(typedArray.getResourceId(R.styleable.DrawableTextView_android_src, 0))
        // 文字
        tvText = TextView(context)
        tvText.id = R.id.tv_text
        tvText.text = typedArray.getString(R.styleable.DrawableTextView_android_text)
        val textSize1 = typedArray.getDimension(R.styleable.DrawableTextView_android_textSize, 8f)
        tvText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize1)
//        tvText.textSize = typedArray.getDimension(R.styleable.DrawableTextView_android_textSize, 8f)
        val textColor = typedArray.getColorStateList(R.styleable.DrawableTextView_android_textColor)
//        textView.setTextColor(if (textColor == null) ColorStateList(arrayOf(), intArrayOf()) else textColor)
        // 上面这行代码可以替换为下面这行代码
        tvText.setTextColor(textColor ?: ColorStateList(arrayOf(), intArrayOf()))

        val drawablePositionCode = typedArray.getInteger(R.styleable.DrawableTextView_drawablePosition, DrawablePosition.BOTTOM.code)
        val drawablePosition = DrawablePosition.getByCode(drawablePositionCode)
        when (drawablePosition) {
            DrawablePosition.LEFT -> {
                // 图片在左方
                // ImageView
                ivImgLayoutParams.topToTop = LayoutParams.PARENT_ID
                ivImgLayoutParams.bottomToBottom = LayoutParams.PARENT_ID
                ivImgLayoutParams.leftToLeft = LayoutParams.PARENT_ID
                ivImgLayoutParams.rightToLeft = tvText.id
                addView(ivImg, ivImgLayoutParams)
                // TextView
                tvTextLayoutParams.topToTop = LayoutParams.PARENT_ID
                tvTextLayoutParams.bottomToBottom = LayoutParams.PARENT_ID
                tvTextLayoutParams.leftToRight = ivImg.id
                tvTextLayoutParams.rightToRight = LayoutParams.PARENT_ID
                addView(tvText, tvTextLayoutParams)
            }
            DrawablePosition.TOP -> {
                // 图片在上方
                // ImageView
                ivImgLayoutParams.topToTop = LayoutParams.PARENT_ID
                ivImgLayoutParams.bottomToTop = tvText.id
                ivImgLayoutParams.leftToLeft = LayoutParams.PARENT_ID
                ivImgLayoutParams.rightToRight = LayoutParams.PARENT_ID
                addView(ivImg, ivImgLayoutParams)
                // TextView
                tvTextLayoutParams.topToBottom = ivImg.id
                tvTextLayoutParams.bottomToBottom = LayoutParams.PARENT_ID
                tvTextLayoutParams.leftToLeft = LayoutParams.PARENT_ID
                tvTextLayoutParams.rightToRight = LayoutParams.PARENT_ID
                addView(tvText, tvTextLayoutParams)
            }
            DrawablePosition.RIGHT -> {
                // 图片在右方
                // ImageView
                ivImgLayoutParams.topToTop = LayoutParams.PARENT_ID
                ivImgLayoutParams.bottomToBottom = LayoutParams.PARENT_ID
                ivImgLayoutParams.leftToRight = tvText.id
                ivImgLayoutParams.rightToRight = LayoutParams.PARENT_ID
                addView(ivImg, ivImgLayoutParams)
                // TextView
                tvTextLayoutParams.topToTop = LayoutParams.PARENT_ID
                tvTextLayoutParams.bottomToBottom = LayoutParams.PARENT_ID
                tvTextLayoutParams.leftToLeft = LayoutParams.PARENT_ID
                tvTextLayoutParams.rightToLeft = ivImg.id
                addView(tvText, tvTextLayoutParams)
            }
            DrawablePosition.BOTTOM -> {
                // 图片在下方
                // ImageView
                ivImgLayoutParams.topToTop = LayoutParams.PARENT_ID
                ivImgLayoutParams.topToBottom = tvText.id
                ivImgLayoutParams.leftToLeft = LayoutParams.PARENT_ID
                ivImgLayoutParams.rightToRight = LayoutParams.PARENT_ID
                addView(ivImg, ivImgLayoutParams)
                // TextView
                tvTextLayoutParams.topToTop = LayoutParams.PARENT_ID
                tvTextLayoutParams.bottomToTop = ivImg.id
                tvTextLayoutParams.leftToLeft = LayoutParams.PARENT_ID
                tvTextLayoutParams.rightToRight = LayoutParams.PARENT_ID
                addView(tvText, tvTextLayoutParams)
            }
        }
        // 未读数
        tvUnreadCount = TextView(context)
        tvUnreadCount.id = R.id.tv_unread_count
        val resourceId = typedArray.getResourceId(R.styleable.DrawableTextView_unreadCountBackground, 0)
        if (resourceId != 0) {
            tvUnreadCount.setBackgroundResource(resourceId)
        } else {
            tvUnreadCount.setBackgroundColor(typedArray.getColor(R.styleable.DrawableTextView_unreadCountBackground, 0))
        }
        tvUnreadCount.gravity = Gravity.CENTER
        setUnreadCount(typedArray.getInt(R.styleable.DrawableTextView_unreadCount, 0))
        val textSize = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_unreadCountTextSize, 8)
        val convertTextSize = DisplayUtil.px2sp(this.context,textSize.toFloat()).toFloat()
        tvUnreadCount.textSize = convertTextSize
        val unreadCountTextColor = typedArray.getColorStateList(R.styleable.DrawableTextView_unreadCountTextColor)
        tvUnreadCount.setTextColor(unreadCountTextColor ?: ColorStateList(arrayOf(), intArrayOf()))
        tvUnreadCount.includeFontPadding = typedArray.getBoolean(R.styleable.DrawableTextView_unreadIncludeFontPadding, false)
        tvUnreadCountLayoutParams.width = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_unreadCountWidth, LayoutParams.WRAP_CONTENT)
        tvUnreadCountLayoutParams.height = typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_unreadCountHeight, LayoutParams.WRAP_CONTENT)
        tvUnreadCountLayoutParams.topToTop = ivImg.id
        tvUnreadCountLayoutParams.bottomToBottom = ivImg.id
        tvUnreadCountLayoutParams.leftToLeft = ivImg.id
        tvUnreadCountLayoutParams.rightToRight = ivImg.id
        tvUnreadCountLayoutParams.circleConstraint = ivImg.id
        tvUnreadCountLayoutParams.circleAngle = 60f
        tvUnreadCountLayoutParams.circleRadius = resources.getDimensionPixelSize(R.dimen.px30)
        addView(tvUnreadCount, tvUnreadCountLayoutParams)

        typedArray.recycle()
    }

    private enum class DrawablePosition(var code: Int) {
        LEFT(0), TOP(1), RIGHT(2), BOTTOM(3);

        companion object {
            fun getByCode(value: Int): DrawablePosition? {
                for (drawablePosition in values()) {
                    if (value == drawablePosition.code) {
                        return drawablePosition
                    }
                }
                return null;
            }
        }
    }

    override fun setSelected(selected: Boolean) {
        super.setSelected(selected)
        for (i in 0 until childCount) {
            getChildAt(i).isSelected = selected
        }
    }

    override fun setEnabled(enabled: Boolean) {
        super.setEnabled(enabled)
        for (i in 0 until childCount) {
            getChildAt(i).isEnabled = enabled
        }
    }

    fun setUnreadCount(unreadCount: Int) {
        if (unreadCount == 0) {
            tvUnreadCount.visibility = View.INVISIBLE
        } else if (unreadCount > 99) {
            tvUnreadCount.visibility = View.VISIBLE
            tvUnreadCount.text = "···"
        } else {
            tvUnreadCount.visibility = View.VISIBLE
            tvUnreadCount.text = "" + unreadCount
        }
    }

}